package org.iipg.common.data.vo;

import org.iipg.common.data.constant.ResponseCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/** Author: zhuhy163@163.com Date: 2020/8/25 */
@ApiModel(value = "Response：通用回复对象")
@Data
public class PhResponse<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "代码", example = "0")
	private int code;

	@ApiModelProperty(value = "消息")
	private String message = "";

	@ApiModelProperty(value = "数据")
	private T data;

	public static <T> PhResponse<T> ok() {
		return restResult(null, ResponseCode.SUCCESS, null);
	}

	public static <T> PhResponse<T> ok(T data) {
		return restResult(data, ResponseCode.SUCCESS, null);
	}

	public static <T> PhResponse<T> ok(T data, String msg) {
		return restResult(data, ResponseCode.SUCCESS, msg);
	}

	public static <T> PhResponse<T> failed() {
		return restResult(null, ResponseCode.FAIL, null);
	}

	public static <T> PhResponse<T> failed(String msg) {
		return restResult(null, ResponseCode.FAIL, msg);
	}

	public static <T> PhResponse<T> failed(T data) {
		return restResult(data, ResponseCode.FAIL, null);
	}

	public static <T> PhResponse<T> failed(T data, String msg) {
		return restResult(data, ResponseCode.FAIL, msg);
	}

	public static <T> PhResponse<T> restResult(T data, int code, String msg) {
		PhResponse<T> apiResult = new PhResponse<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMessage(msg);
		return apiResult;
	}

}
